package youcaihuaoa.weetop.com.im;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import io.rong.imkit.config.RongConfigCenter;
import io.rong.imkit.conversationlist.model.BaseUiConversation;
import io.rong.imkit.conversationlist.provider.BaseConversationProvider;
import io.rong.imkit.utils.RongDateUtils;
import io.rong.imkit.utils.RongUtils;
import io.rong.imkit.widget.adapter.IViewProviderListener;
import io.rong.imkit.widget.adapter.ViewHolder;
import io.rong.imlib.IRongCoreEnum;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import youcaihuaoa.weetop.com.R;

public class CustomConversationProvider extends BaseConversationProvider {

    @Override
    public boolean isItemViewType(BaseUiConversation item) {
        //根据业务需要，判断 item 是该模板需要处理的会话时，返回 true, 否则返回 false
        //此处以自定义私聊会话模板为例
        return item.mCore.getConversationType().equals(Conversation.ConversationType.PRIVATE);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //根据业务需要，自定义处理
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void bindViewHolder(final ViewHolder holder, final BaseUiConversation uiConversation, int position, List<BaseUiConversation> list, IViewProviderListener<BaseUiConversation> listener) {
        holder.setText(R.id.rc_conversation_title, uiConversation.mCore.getConversationTitle());
        int unreadCount;
        if (!TextUtils.isEmpty(uiConversation.mCore.getPortraitUrl())) {
            RongConfigCenter.featureConfig().getKitImageEngine().loadConversationListPortrait(holder.getContext(), uiConversation.mCore.getPortraitUrl(), (ImageView) holder.getView(R.id.rc_conversation_portrait), uiConversation.mCore);
        } else {
            unreadCount = R.drawable.rc_default_portrait;
            if (uiConversation.mCore.getConversationType().equals(Conversation.ConversationType.GROUP)) {
                unreadCount = R.drawable.rc_default_group_portrait;
            } else if (uiConversation.mCore.getConversationType().equals(Conversation.ConversationType.CHATROOM)) {
                unreadCount = R.drawable.rc_default_chatroom_portrait;
            } else if (uiConversation.mCore.getConversationType().equals(Conversation.ConversationType.CUSTOMER_SERVICE)) {
                unreadCount = R.drawable.rc_default_chatroom_portrait;
            }

            Uri uri = RongUtils.getUriFromDrawableRes(holder.getContext(), unreadCount);
            RongConfigCenter.featureConfig().getKitImageEngine().loadConversationListPortrait(holder.getContext(), uri.toString(), (ImageView) holder.getView(R.id.rc_conversation_portrait), uiConversation.mCore);
        }

        holder.getView(R.id.rc_conversation_portrait).setOnClickListener(v -> {
            if (RongConfigCenter.conversationListConfig().getListener() != null) {
                RongConfigCenter.conversationListConfig().getListener().onConversationPortraitClick(holder.getContext(), uiConversation.mCore.getConversationType(), uiConversation.mCore.getTargetId());
            }

        });
        holder.getView(R.id.rc_conversation_portrait).setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                return RongConfigCenter.conversationListConfig().getListener() != null ? RongConfigCenter.conversationListConfig().getListener().onConversationPortraitLongClick(holder.getContext(), uiConversation.mCore.getConversationType(), uiConversation.mCore.getTargetId()) : false;
            }
        });
        ((TextView) holder.getView(R.id.rc_conversation_content)).setCompoundDrawables((Drawable) null, (Drawable) null, (Drawable) null, (Drawable) null);
        if (uiConversation.mCore.getSentStatus() != null && TextUtils.isEmpty(uiConversation.mCore.getDraft()) && !TextUtils.isEmpty(uiConversation.mConversationContent)) {
            Drawable drawable = null;
            if (uiConversation.mCore.getSentStatus() == Message.SentStatus.FAILED) {
                drawable = holder.getContext().getResources().getDrawable(R.drawable.rc_ic_warning);
            } else if (uiConversation.mCore.getSentStatus() == Message.SentStatus.SENDING) {
                drawable = holder.getContext().getResources().getDrawable(R.drawable.rc_conversation_list_msg_sending);
            }

            if (drawable != null) {
                Bitmap bitmap = BitmapFactory.decodeResource(holder.getContext().getResources(), R.drawable.rc_ic_warning);
                int width = bitmap.getWidth();
                drawable.setBounds(0, 0, width, width);
                ((TextView) holder.getView(R.id.rc_conversation_content)).setCompoundDrawablePadding(10);
                ((TextView) holder.getView(R.id.rc_conversation_content)).setCompoundDrawables(drawable, (Drawable) null, (Drawable) null, (Drawable) null);
            }
        }

        holder.setText(R.id.rc_conversation_content, uiConversation.mConversationContent, TextView.BufferType.SPANNABLE);
        unreadCount = uiConversation.mCore.getUnreadMessageCount();
        String count;
        if (unreadCount > 0) {
            holder.setVisible(R.id.rc_conversation_unread, true);
            if (unreadCount > 99) {
                holder.setImageResource(R.id.rc_conversation_unread_bg, R.drawable.rc_unread_count_bg_large);
                holder.setText(R.id.rc_conversation_unread_count, holder.getContext().getString(R.string.rc_conversation_unread_dot));
            } else {
                holder.setImageResource(R.id.rc_conversation_unread_bg, R.drawable.rc_unread_count_bg_normal);
                count = Integer.toString(unreadCount);
                holder.setText(R.id.rc_conversation_unread_count, count);
            }
        } else {
            holder.setVisible(R.id.rc_conversation_unread, false);
        }

        count = RongDateUtils.getConversationListFormatDate(uiConversation.mCore.getSentTime(), holder.getContext());
        holder.setText(R.id.rc_conversation_date, count);
        if (uiConversation.mCore.isTop()) {
            holder.getConvertView().setBackgroundColor(holder.getContext().getResources().getColor(R.color.rc_item_top_color));
        } else {
            holder.getConvertView().setBackgroundColor(holder.getContext().getResources().getColor(R.color.rc_white_color));
        }

        boolean noDisturb = uiConversation.mCore.getNotificationStatus().equals(Conversation.ConversationNotificationStatus.DO_NOT_DISTURB);
        holder.setVisible(R.id.rc_conversation_no_disturb, noDisturb);
        if (Conversation.ConversationType.ULTRA_GROUP.equals(uiConversation.mCore.getConversationType())) {
            holder.setVisible(R.id.divider, false);
        } else {
            holder.setVisible(R.id.divider, true);
        }
        holder.setVisible(R.id.rc_conversation_portrait_group, uiConversation.mCore.getConversationType().equals(Conversation.ConversationType.GROUP));
        holder.setVisible(R.id.rc_conversation_portrait, !uiConversation.mCore.getConversationType().equals(Conversation.ConversationType.GROUP));
        holder.setText(R.id.rc_conversation_portrait_group, uiConversation.mCore.getConversationTitle().substring(0, 1));

        if (isDebugMode(holder.getContext())) {
            if (uiConversation.mCore.getConversationType() == Conversation.ConversationType.ULTRA_GROUP && uiConversation.mCore.getChannelType() != null) {
                holder.setText(R.id.rc_conversation_title, uiConversation.mCore.getConversationTitle() + (uiConversation.mCore.getChannelType() == IRongCoreEnum.UltraGroupChannelType.ULTRA_GROUP_CHANNEL_TYPE_PRIVATE ? "(私)" : "(公)") + "(" + uiConversation.mCore.getPushNotificationLevel() + ")");
            } else {
                holder.setText(R.id.rc_conversation_title, uiConversation.mCore.getConversationTitle() + "(" + uiConversation.mCore.getPushNotificationLevel() + ")");
            }
        }

    }

    private boolean isDebugMode(Context context) {
        return context.getSharedPreferences("config", 0).getBoolean("isDebug", false);
    }
}
